<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.text.ParseException" %>
<%@page import="java.util.ArrayList"%>
<%@page import="com.java.FamTree.*" %>
<%@ include file="loginDetails.jsp" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<HTML>
<BODY>

<%!
String personId = null;
String userId = null;

String Bio = null;
String BirthDate = null;
String BirthPlace = null;
String Name = null;
String Gender = null;
String LastKnownAddress = null;
String DeathDate = null;
String DeathPlace = null;

String Bio1 = null;
String BirthDate1 = null;
String BirthPlace1 = null;
String Name1 = null;
String Gender1 = null;
String LastKnownAddress1 = null;
String DeathDate1 = null;
String DeathPlace1 = null;

ArrayList<Field> lastEditFields = new ArrayList<Field>();
Field[] editFields = new Field[8];
boolean success = false;
boolean updatePersonInformation(JspWriter out,HttpServletRequest request,
					HttpSession session)
{
	personId = request.getParameter("PersonId");
	userId = request.getParameter("UserId");
	
	Bio = request.getParameter("Bio");
	BirthDate = request.getParameter("BirthDate");
	BirthPlace = request.getParameter("BirthPlace");
	Name = request.getParameter("Name");
	Gender = request.getParameter("Gender");
	LastKnownAddress = request.getParameter("LastKnownAddress");
	DeathDate = request.getParameter("BirthDate");
	DeathPlace = request.getParameter("DeathPlace");
	
	Bio1 = request.getParameter("Bio1");
	BirthDate1 = request.getParameter("BirthDate1");
	BirthPlace1 = request.getParameter("BirthPlace1");
	Name1 = request.getParameter("Name1");
	Gender1 = request.getParameter("Gender1");
	LastKnownAddress1 = request.getParameter("LastKnownAddress1");
	DeathDate1 = request.getParameter("BirthDate1");
	DeathPlace1 = request.getParameter("DeathPlace1");
	
    
	if(personId == null)
		return false;
	// Should make a database connection here and check password
	try { // Load driver class
		Class.forName("com.mysql.jdbc.Driver");
	}
	catch (java.lang.ClassNotFoundException e) {
		System.err.println("ClassNotFoundException: " +e);
	}
	Connection con = null;
	try { 
		con = DriverManager.getConnection(url+db, uid, pwd); 
		PreparedStatement updatePerson = con.prepareStatement("Update Person SET Bio=?, BirthDate=?, BirthPlace=?, Name=?, Gender=?, LastKnownAddress=?, DeathDate=?, DeathPlace=? WHERE PersonId=?");
		updatePerson.setString(1, Bio);
		SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd");
		String temp2 = BirthDate;
		try{
			f1.parse(temp2);
		}catch(Exception e){
			temp2 = null;
		}
		updatePerson.setDate(2, temp2 == null ? null :  new java.sql.Date(f1.parse(temp2).getTime()));
		updatePerson.setString(3, BirthPlace);
		updatePerson.setString(4, Name);
		updatePerson.setInt(5, Integer.parseInt(Gender));
		updatePerson.setString(6, LastKnownAddress);
		SimpleDateFormat f2 = new SimpleDateFormat("yyyy-MM-dd");
		String temp1 = DeathDate;
		try{
			f2.parse(temp1);
		}catch(Exception e){
			temp1 = null;
		}
		updatePerson.setDate(7, temp1 == null ? null : new java.sql.Date(f2.parse(temp1).getTime()));
		updatePerson.setString(8, DeathPlace);
		updatePerson.setInt(9, Integer.parseInt(personId));
		
        updatePerson.executeUpdate();
		
	} catch (SQLException ex) { 
	    System.err.println(ex); 
	    success= false;
	} catch(ParseException e){}finally {
	    if (con != null ) {
	        try {
	            con.close();
	            success = true;
	        } catch (SQLException ex) {
	            System.err.println(ex);
	            success= false;
	        }
	    }
	}
	
	/*
	* Now see what fields were edited and update teh Field table
	*/
	
	ArrayList<Integer> newEdits = new ArrayList<Integer>();
	if(!Bio.equals(Bio1)){
		newEdits.add(7);
	}
	if(BirthDate1 == null){BirthDate1 = "null";}
	if(!BirthDate.equals(BirthDate1)){
		newEdits.add(8);
	}
	if(!BirthPlace.equals(BirthPlace1)){
		newEdits.add(9);
	}
	if(!Name.equals(Name1)){
		newEdits.add(10);
	}
	if(!Gender.equals(Gender1)){
		newEdits.add(11);
	}
	if(!LastKnownAddress.equals(LastKnownAddress1)){
		newEdits.add(12);
	}
	if(DeathDate1 == null){DeathDate1 = "null";}
	if(!DeathDate.equals(DeathDate1)){
		newEdits.add(13);
	}
	if(!DeathPlace.equals(DeathPlace1)){
		newEdits.add(14);
	}
	
	for(int f : newEdits){
		Connection con2 = null;
		try { 
			con2 = DriverManager.getConnection(url+db, uid, pwd); 
			PreparedStatement deleteField = con2.prepareStatement("DELETE FROM Field WHERE FieldId=? AND Person=?");
			deleteField.setInt(1, f);
			deleteField.setInt(2, Integer.parseInt(personId));
			deleteField.executeUpdate();
			PreparedStatement updateField = con2.prepareStatement("INSERT INTO Field (FieldId, LastEditDate, Person, UserId) VALUES(?, CURRENT_DATE(), ?, ?)");
			updateField.setInt(1, f);
			updateField.setInt(2, Integer.parseInt(personId));
			updateField.setInt(3, Integer.parseInt(userId));
			
			updateField.executeUpdate();
			
		} catch (SQLException ex) { 
		    System.err.println(ex); 
		    success= false;
		} finally {
		    if (con2 != null ) {
		        try {
		            con2.close();
		            success = true;
		        } catch (SQLException ex) {
		            System.err.println(ex);
		            success= false;
		        }
		    }
		}
	}
	
	return true;
} 
%>


<%
	//Check to see if we're logged in.
	if (session.getAttribute("UserId") == null) {
	    response.sendRedirect("index.jsp"); // Send them away if they're not logged in.
	}


	boolean success = false;
	session = request.getSession(true);// May create new session
	//try{
		success = updatePersonInformation(out,request,session);
	//}
	//catch(IOException e){ System.err.println(e); }
	
	if(!success)
		response.sendRedirect("ViewIndividual.jsp?PersonId="+personId); // Success
	else
		response.sendRedirect("ViewIndividual.jsp?PersonId="+personId); // Failed login
	// Redirect back to login page with a message
%>

</BODY>
</HTML>